package com.iaiai.cobra.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iaiai.cobra.common.constant.YesOrNo;
import com.iaiai.cobra.common.util.StringUtil;
import com.iaiai.cobra.common.vo.ResultVo;
import com.iaiai.cobra.core.BaseController;
import com.iaiai.cobra.repository.bean.Dept;
import com.iaiai.cobra.repository.bean.Post;
import com.iaiai.cobra.web.params.PageParams;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * Created with DeptController.
 * Package: com.iaiai.cobra.system.controller
 * Author: iaiai
 * Create Time: 2024/6/28 20:08
 * QQ: 176291935
 * Email: 176291935@qq.com
 * Description:
 */
@RestController
@RequestMapping("/dept")
public class DeptController extends BaseController {

    @PostMapping("/list.json")
    public ResultVo page(@RequestBody PageParams params) {
        if(params==null)new PageParams();
        if(params.getPage()==null || params.getLimit()==null){
            return fail("参数异常");
        }

        Page<Dept> pages = serviceManager.deptService.queryPage(new Page(params.getPage(),params.getLimit()),params.getSearch());
        return success(pages);
    }

    @PostMapping("/add.do")
    public ResultVo add(@RequestBody Dept params) {
        if(params==null || StringUtil.isEmpty(params.getName()) || params.getDisabled()==null){
            return fail("参数异常");
        }

        Dept maxDept = serviceManager.deptService.queryMax();

        params.setDel(YesOrNo.NO.getValue());
        params.setCreateTime(new Date());
        params.setModifyTime(params.getCreateTime());
        params.setSeq(maxDept==null ? 0 : (maxDept.getSeq().intValue()+1));
        serviceManager.deptService.save(params);

        return success();
    }

    @PostMapping("/detail.json")
    public ResultVo detail(@RequestBody Post params) {
        if(params==null || params.getId()==null){
            return fail("参数异常");
        }
        return success(serviceManager.deptService.getById(params.getId()));
    }

    @PostMapping("/edit.do")
    public ResultVo edit(@RequestBody Dept params) {
        if(params==null
                || params.getId()==null
                || StringUtil.isEmpty(params.getName())
                || params.getDisabled()==null){
            return fail("参数异常");
        }

        Dept dept = serviceManager.deptService.getById(params.getId());
        dept.setModifyTime(new Date());
        dept.setName(params.getName());
        dept.setRemark(params.getRemark());
        dept.setDisabled(params.getDisabled());
        serviceManager.deptService.updateById(dept);

        return success();
    }

    @PostMapping("/del.do")
    public ResultVo del(@RequestBody Dept params) {
        if(params==null || params.getId()==null){
            return fail("参数异常");
        }

        Dept dept = serviceManager.deptService.getById(params.getId());
        dept.setModifyTime(new Date());
        dept.setDel(YesOrNo.YES.getValue());
        serviceManager.deptService.updateById(dept);

        return success();
    }

}
